/*
 * Book.java
 *
 * This class demonstrates the use of two-dimensionsal arrays to store the
 * ISBN number, year of publicaton and number of pages in a book.
 *

 */

package com.javacore.day4.bookdetails;

import java.util.Scanner;

/**
 * Book class stores information about a book in a two-dimensional array and
 * allows the user to add, search and display book information.
 * @author 
 */
public class Book {
    
    /**
     * Two-dimensional array to store ISBN number, number of pages and year of
     * publication of a book. This array acts as a repository of books.
     */
    int books[][];
    
    /**
     * Instance variable to store the number of books added to the repository.
     */
    byte bookCount;
    
    
    /** Creates a new instance of Book */
    public Book() {
        books = new int[3][3];
        bookCount = 0;
    }
    
    /**
     * Method to add a book to the repository
     */
    void addBook(){
        Scanner input = new Scanner(System.in);
        
        // Add a book only if there is space in the array.
        if(bookCount < books.length){
            
            // Accept the ISBN number
            System.out.println("Enter the ISBN number of the book:");
            books[bookCount][0] = input.nextInt();
            
            // Accept the number of pages in the book
            System.out.println("Enter the number of pages in the book:");
            books[bookCount][1] = input.nextInt();
            
            // Accept the year of publication
            System.out.println("Enter the year of publication:");
            books[bookCount][2] = input.nextInt();
            
            // Increment the number of books in the repository
            bookCount++;
        } else
            System.out.println("Sorry! Unable to add more books.\n");
    }
    
    /**
     * Method to display information about all the books in the repository
     */
    void displayBooks(){
        
        // Display books only if the repository is not empty
        if(bookCount > 0) {
            System.out.println("\n ISBN \t\tPages \t Publication Year");
            System.out.println("-------------------------------------");
            
            // Iterate until all the book details are displayed
            for(int rowIndex = 0;rowIndex < bookCount;rowIndex++) {
                System.out.printf("%d\t\t", books[rowIndex][0]);
                System.out.printf("%d\t\t", books[rowIndex][1]);
                System.out.printf("%d\n", books[rowIndex][2]);
            }
        } else
            System.out.println("No books to display");
    }
    
    /**
     * Method to search a book by ISBN number
     */
    void searchByIsbn(){
        
        // Variable to indicate if the search was successful
        boolean bookFound = false;
        
        // Search books only if the repository is not empty
        if(bookCount > 0) {
            Scanner input = new Scanner(System.in);
            System.out.println("\nEnter the ISBN number of the book you want to " +
                    "search for:");
            int searchIsbn = input.nextInt();
            System.out.println(" ISBN \t\tPages \t Publication Year");
            System.out.println("-------------------------------------");
            
            // Iterate until a matching ISBN is found
            for(int rowIndex = 0;rowIndex < bookCount;rowIndex++) {
                if(books[rowIndex][0] == searchIsbn) {
                    System.out.printf("%d\t\t", books[rowIndex][0]);
                    System.out.printf("%d\t\t", books[rowIndex][1]);
                    System.out.printf("%d\n", books[rowIndex][2]);
                    bookFound = true;
                    break;
                }
            }
            
            // Display a message if a book is not found
            if(!bookFound)
                System.out.println("Book not found.");
        } else
            System.out.println("No books to search");
    }
    
    /**
     * Search for a record by year of publication
     */
    void searchByYear(){
        
        // Variable to indicate if the search was successful
        boolean bookFound = false;
        
        // Search books only if the repository is not empty
        if(bookCount > 0) {
            Scanner input = new Scanner(System.in);
            System.out.println("\nEnter the year of publication of the book " +
                    "you want to search for:");
            int publicationYear = input.nextInt();
            System.out.println(" ISBN \t\tPages \t Publication Year");
            System.out.println("-------------------------------------");
            
            // Iterate to display all books matching the publishing year
            // specified by the user
            for(int rowIndex = 0;rowIndex < bookCount;rowIndex++){
                if(books[rowIndex][2] == publicationYear) {
                    System.out.printf("%d\t\t", books[rowIndex][0]);
                    System.out.printf("%d\t\t", books[rowIndex][1]);
                    System.out.printf("%d\n", books[rowIndex][2]);
                    // If a match is found, then set the flag to true
                    bookFound = true;
                }
            }           
            
            // Display a message if a book is not found
            if(!bookFound)
                System.out.println("Book not found.");
        } else
            System.out.println("No books to search.");
    }
}